package cn.sfa.common.interceptor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sfa.common.utils.CommonUtil;

/**
 * 
* 类名称：XmlURLMerge   
* 类描述：xml文件URL合并
* 创建人：lenovo   
* 创建时间：2012-2-27 下午3:16:47   
* 修改人：lenovo   
* 修改时间：2012-2-27 下午3:16:47   
* 修改备注：   
* @version
 */
public class XmlURLMerge {
	
	static Logger logger = LoggerFactory.getLogger(XmlURLMerge.class);

	public static List<XmlBean> getXmlUrlList() {
		
		//获取的path为:eg:file:/D:/apache-tomcat-6.0.29-8888/webapps/sfa/WEB-INF/classes//menuxml
		String path = XmlURLMerge.class.getClassLoader().getResource("/").toString()+"//menuxml";
		
		path = path.substring(5);//去掉"file:/"

		//path = path.replace("%20", " ");
		
		List<XmlBean> list = new ArrayList<XmlBean>();

		File file = new File(path.trim());
		
		String[] xmlFile=null;//xml文件数组
		
		if(file!=null && file.isDirectory()){

			xmlFile = file.list();
			//解析每一个xml的URL链接地址及level等级
			
			String url="";
			String level="";
			XmlBean xmlBean = null ;
			
			for(String xf : xmlFile){
			
				try {
					//建产一个xml文档的解析器
					SAXReader reader = new SAXReader();
					
					
					String path2 = path+File.separator+xf;
					
					//通过解析器读取文档
					Document  document = reader.read(path2);
					
					//查找所有的url
					
					
					//1.查找所有的拥有url标签
					List<Element> urlList = document.selectNodes("//url");
					//如果urlList不为空
					if(!CommonUtil.isEmpty(urlList) && urlList.size()>0 ){
						//循环该list,把相应属性封装到XmlBean里面
						for(Element e : urlList){
							url = e.attributeValue("link");
							level = e.attributeValue("level");
							xmlBean = new XmlBean();
							xmlBean.setURL(url);
							xmlBean.setLevel(level);
							list.add(xmlBean);
						}
						
					}
					//2.查找所有的拥有属性为url标签的元素
					List<Element> urlList2 = document.selectNodes("//Column[@Url]");
					//如果urlList2不为空
					if(!CommonUtil.isEmpty(urlList2) && urlList2.size()>0){
						//循环该list,把相应属性封装到XmlBean里面
						for(Element e2 : urlList2){
							url = e2.attributeValue("Url");
							level = e2.attributeValue("level");
							xmlBean = new XmlBean();
							xmlBean.setURL(url);
							xmlBean.setLevel(level);
							list.add(xmlBean);
						}
						
					}
					
					//3.查找所有的导航的url标签元素
					List<Element> urlList3 = document.selectNodes("//Navigation[@Url]");
					//如果urlList3不为空
					if(!CommonUtil.isEmpty(urlList3) && urlList3.size()>0){
						//循环该list,把相应属性封装到XmlBean里面
						for(Element e2 : urlList3){
							url = e2.attributeValue("Url");
							level = e2.attributeValue("level");
							xmlBean = new XmlBean();
							xmlBean.setURL(url);
							xmlBean.setLevel(level);
							list.add(xmlBean);
						}
						
					}
					
				} catch (DocumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			}
		}
		return list;
	}


}
